package com.application.sistercityapplication.news;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.sistercityapplication.dto.NewsDto;
import com.application.sistercityapplication.main.R;
import com.application.sistercityapplication.utils.DateUtil;
import com.squareup.picasso.Picasso;

public class NewsListAdapter extends BaseAdapter {
	private List<NewsDto> _list;
	private Context _context;
	private Activity _activity;
	private ViewHolder viewHolder;
	private RelativeLayout clicklayout;
	private NewsDto newsDto;

	public NewsListAdapter(List<NewsDto> list, Context con, Activity ac) {
		// TODO Auto-generated constructor stub
		this._list = list;
		this._context = con;
		this._activity = ac;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return _list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return _list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if(convertView== null){
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(R.layout.news_list_each_item, null);
			viewHolder.countryName = (TextView) convertView.findViewById(R.id.countryName);
			viewHolder.newsName = (TextView) convertView.findViewById(R.id.text_item);
			viewHolder.pubDate = (TextView) convertView.findViewById(R.id.pubDate);
			viewHolder.newsImage = (ImageView) convertView.findViewById(R.id.imageView2);
			clicklayout = (RelativeLayout) convertView.findViewById(R.id.click_relative);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		newsDto = _list.get(position);
		try{
			/*
			 * getImage from dummy
			 */
			
			viewHolder.countryName.setText(newsDto.getCountry_name());
			viewHolder.newsName.setText(newsDto.getTitle());
			

			viewHolder.pubDate.setText(DateUtil.getDateStr(newsDto.getCreated_at()));
			if(newsDto.getImage_path()!=null && !"".equals(newsDto.getImage_path())){
				Picasso.with(_context).load(newsDto.getImage_path()).into(viewHolder.newsImage);
			}
			clicklayout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					newsDto = _list.get(position);
					Intent i = new Intent(_activity,FullNewsMainActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					i.putExtra("news", newsDto);
					_activity.startActivity(i);
					_activity.overridePendingTransition(R.anim.slide_in_from_bot, R.anim.slide_in_from_bot);
					//_activity.finish();
				}
			});
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return convertView;
	}

	public class ViewHolder {
		private ImageView newsImage;
		private TextView newsName;
		private TextView pubDate;
		private TextView countryName;
	}

}
